<?php

namespace app\Modules\Api\models;

use Yii;
use app\models\Showorder as BaseModel;
use yii\helpers\ArrayHelper;
/**
 * LoginForm is the model behind the login form.
 *
 * @property user|null $user This property is read-only.
 *
 */
class Showorder extends BaseModel
{
    public function fields()
    {
        $fields = parent::fields();
        $strvalfields = [
            'showid',
            'order_id',
            'product_id',
            'auction_id',
            'user_id',
        ];
        $fields['uimg'] = function(){
            $pic = $this->uimg?$this->uimg:'default_photo.jpg';
            return \Yii::$app->params['uploadUrl'].$pic;
        };
        foreach($strvalfields as $strvalfield){
            $fields[$strvalfield] = function() use ($strvalfield){
                return strval($this->$strvalfield);
            };
        }
        $dateFields = ['created_at','updated_at'];
        foreach($dateFields as $dateField){
            $fields[$dateField] = function() use($dateField){
                return Date('Y-m-d G:i:s',$this->$dateField);
            };
        }
        return $fields;
    }

    public function extraFields()
    {
        return [
            'showpics'=>function(){
                $showpics = ArrayHelper::getColumn(Showpic::find()->where(['show_id'=>$this->showid])->orderBy(['showpicid'=>SORT_DESC])->all(),function($element){
                    return Yii::$app->params['uploadUrl'].$element['url'];
                });
                return $showpics;
            }
        ];
    }
}